package com.amikomgamedev.labirinhantu.state;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import com.amikomgamedev.labirinhantu.Server_Data;
import com.amikomgamedev.labirinhantu.Utils;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;

public class State_Game_Over
	extends
	    State_Activity
{
    protected void create_Game_Over()
    {
	grafik.lst_Game_Over_Bg.get(0).setVisible(true);
	grafik.spr_Game_Over_Darah.animate(GAME_PLAY_BLOOD_DUR, 0, 6, 0);
	sound.playGameSfx(SFX_GAME_DARAH);
	sound.playGameSfx(SFX_GAME_OVER);
	sound.stopGameBgm(Server_Data.getStage());
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
	if (State_Menu_Current == STATE_GAME_OVER)
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK)
		finish();
	}

	return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onAreaTouched(
	    TouchEvent pSceneTouchEvent,
	    ITouchArea pTouchArea,
	    float pTouchAreaLocalX,
	    float pTouchAreaLocalY)
    {
	if (State_Menu_Current == STATE_GAME_OVER)
	{
	    if (pSceneTouchEvent.isActionUp())
	    {
		if (pTouchArea.equals(grafik.lst_Game_Over_Btn.get(0)))
		{
		    sound.playGameBgm(Server_Data.getStage());
		    detachGame();
		}
		
		if (pTouchArea.equals(grafik.lst_Game_Over_Btn.get(1)))
		{
		    sound.playGameBgm(Server_Data.getStage());
		    restartGame();
		}
		
		if (pTouchArea.equals(grafik.spr_Icon_Social_Game))
		{
		    	startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/cupcorngames")));
		}
	    }

	}

	return super.onAreaTouched(
		pSceneTouchEvent,
		pTouchArea,
		pTouchAreaLocalX,
		pTouchAreaLocalY);
    }

    protected void detachGame()
    {
	runOnUpdateThread(new Runnable()
	{
	    public void run()
	    {
		grafik.detachMenuLevelBg();
		grafik.detachMenuLevelBtn();
		grafik.detachMenuLevel();
//		grafik.attachMenuLevelGembok();
//		grafik.attachMenuLevelTulang();
		grafik.detachMenuLevelLingkaran();
		teks.detachMenuLevel();
		
		grafik.detachGamePlayBg();
		grafik.detachGamePlayMc();
		grafik.detachGamePlayBtn();
		grafik.detachGamePlayCollection();
		grafik.detachGamePlayFloat();
		grafik.detachGamePlayMist();
		grafik.detachGamePlayFire();
		grafik.detachGamePlayGhost();
		grafik.detachGamePlayWater();
		grafik.detachGamePlayMoveGhost();
		grafik.detachGamePlayBtnGhost(); 
		grafik.detachGamePlayBtnMist();
		grafik.detachGamePlayFinish();
		grafik.detachGamePlayWall();
		grafik.detachGamePlayTaskbar();
		grafik.detachGamePlayItemLeft();
		grafik.detachGamePlayItemRight();
		grafik.detachGamePause();
		grafik.detachGameOver();
		teks.detachClue();
		teks.detachRule();
//		teks.detachTime();

		if (grafik.spr_Game_Over_Txt.getParent() == null)
		    switchState(STATE_MENU_LEVEL);
	    }
	});
    }

    protected void restartGame()
    {
	runOnUpdateThread(new Runnable()
	{
	    public void run()
	    {
		grafik.detachGamePlayBg();
		grafik.detachGamePlayMc();
		grafik.detachGamePlayBtn();
		grafik.detachGamePlayCollection();
		grafik.detachGamePlayFloat();
		grafik.detachGamePlayMist();
		grafik.detachGamePlayFire();
		grafik.detachGamePlayGhost();
		grafik.detachGamePlayWater();
		grafik.detachGamePlayMoveGhost();
		grafik.detachGamePlayBtnGhost(); 
		grafik.detachGamePlayBtnMist();
		grafik.detachGamePlayFinish();
		grafik.detachGamePlayWall();
		grafik.detachGamePlayTaskbar();
		grafik.detachGamePause();
		grafik.detachGameOver();
		teks.detachRule();
		teks.detachClue();
//		teks.detachTime();

		if (grafik.spr_Game_Over_Txt.getParent() == null)
		    switchState(STATE_GAME_PLAY);
	    }
	});
    }
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_GAME_OVER)
		sound.stopGameBgm(Server_Data.getStage());
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_GAME_OVER)
		sound.playGameBgm(Server_Data.getStage());

	    super.onResumeGame();
	}
}
